package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAffiliatedUnits;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAffiliatedUnitsRel;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAffiliatedUnitsRelService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAffiliatedUnitsService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.AffiiationEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationStatusInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Description：挂靠状态停用MQ消息接收处理类</p>
 *
 * @author panhj
 * @version 1.0
 * @createDate 2020-07-01
 */
@AllArgsConstructor
@Slf4j
@Component
public class AffiliationStatusProcessor implements AfsMqBizProcessor<AffiliationStatusInfo> {

    private final ChannelAffiliatedUnitsService channelAffiliatedUnitsService;
    private final ChannelAffiliatedUnitsRelService channelAffiliatedUnitsRelService;

    @Override
    public boolean processMessage(AffiliationStatusInfo entity) throws Exception {
        this.AffiliationStatusQueue(entity);
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_CHANNEL_CHANNEL_CASE_CTM_UPDATE_STATUS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String AffiliationStatusQueue(AffiliationStatusInfo affiliationStatusInfo) {
        String errorMsg = "";
        try {
            log.info("开始同步挂靠状态信息……", affiliationStatusInfo);

            ArrayList<ChannelAffiliatedUnits> channelAffiliatedUnitsArrayList = this.channelAffiliatedUnitsArrayList(affiliationStatusInfo);
            if (channelAffiliatedUnitsArrayList != null && channelAffiliatedUnitsArrayList.size() > 0) {
                channelAffiliatedUnitsService.saveOrUpdateBatch(channelAffiliatedUnitsArrayList, channelAffiliatedUnitsArrayList.size());
                for (int i = 0; i < channelAffiliatedUnitsArrayList.size(); i++) {
                    List<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsRel = channelAffiliatedUnitsRelService.list(Wrappers.<ChannelAffiliatedUnitsRel>query().lambda()
                            .eq(ChannelAffiliatedUnitsRel::getArriliatedId, channelAffiliatedUnitsArrayList.get(i).getId()));
                    for (int j = 0; j < channelAffiliatedUnitsRel.size(); j++) {
                        channelAffiliatedUnitsRel.get(j).setStatus(AffiiationEnum.STOP.getCode());
                        channelAffiliatedUnitsRelService.updateById(channelAffiliatedUnitsRel.get(j));
                    }
                }
            }
            log.info("########开始同步挂靠状态信息########", affiliationStatusInfo);

        } catch (Exception e) {
            errorMsg = e.getMessage();
            log.error("同步挂靠状态信息失败", affiliationStatusInfo, e);
        }
        return errorMsg;
    }

    /**
     * 解析挂靠信息
     */
    private ArrayList<ChannelAffiliatedUnits> channelAffiliatedUnitsArrayList(AffiliationStatusInfo affiliationStatusInfo) {
        ArrayList<ChannelAffiliatedUnits> channelAffiliatedUnitsArrayList = new ArrayList<>();
        if (affiliationStatusInfo.getAffiliationDtoArrayList() != null) {
            affiliationStatusInfo.getAffiliationDtoArrayList().forEach(AffiliationDto -> {
                ChannelAffiliatedUnits channelAffiliatedUnits = channelAffiliatedUnitsService.getOne(Wrappers.<ChannelAffiliatedUnits>query().lambda()
                        .eq(ChannelAffiliatedUnits::getId, AffiliationDto.getId()));
                if (channelAffiliatedUnits == null) {
                    channelAffiliatedUnits = new ChannelAffiliatedUnits();
                }
                BeanUtil.copyProperties(AffiliationDto, channelAffiliatedUnits, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                channelAffiliatedUnitsArrayList.add(channelAffiliatedUnits);
            });
        }
        return channelAffiliatedUnitsArrayList;
    }

}
